﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.Model;
using System.Data.SqlClient;
using Register.DBUtility;
using System.Data;
using Register.IDAL;

namespace Register.DAL
{
    public class UserRoleDAL : IUserRole
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int  Add(UserRole model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_userRole(");
            strSql.Append("Role_ID,Administrator_ID,UserRole_Describe,UserRole_Addtime,UserRole_Recoder,UserRole_LastAmendTime,UserRole_LastAmendPerson)");
            strSql.Append(" values (");
            strSql.Append("@Role_ID,@Administrator_ID,@UserRole_Describe,@UserRole_Addtime,@UserRole_Recoder,@UserRole_LastAmendTime,@UserRole_LastAmendPerson)");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType.Char,5),
					new SqlParameter("@Administrator_ID", SqlDbType.Char,14),
					new SqlParameter("@UserRole_Describe", SqlDbType.VarChar,100),
					new SqlParameter("@UserRole_Addtime", SqlDbType.DateTime),
					new SqlParameter("@UserRole_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@UserRole_LastAmendTime", SqlDbType.DateTime),
					new SqlParameter("@UserRole_LastAmendPerson", SqlDbType.VarChar,20)};
            parameters[0].Value = model.Role_ID;
            parameters[1].Value = model.Administrator_ID;
            parameters[2].Value = model.UserRole_Describe;
            parameters[3].Value = model.UserRole_Addtime;
            parameters[4].Value = model.UserRole_Recoder;
            parameters[5].Value = model.UserRole_LastAmendTime;
            parameters[6].Value = model.UserRole_LastAmendPerson;

          return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<UserRole> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Role_ID,Administrator_ID,UserRole_Describe,UserRole_Addtime,UserRole_Recoder,UserRole_LastAmendTime,UserRole_LastAmendPerson ");
            strSql.Append(" FROM System_userRole ");
            strSql.Append(" where  Administrator_ID=@Administrator_ID");
             SqlParameter[] parameters = {
					new SqlParameter("@Administrator_ID", SqlDbType .NVarChar,14)
                                            };
             parameters[0].Value = strWhere;
             return ChangeReaderToList(DbHelperSQL.Query(strSql.ToString(), parameters));
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Role_ID, string Administrator_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_userRole ");
            strSql.Append(" where Role_ID=@Role_ID and Administrator_ID=@Administrator_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType.Char,5),
					new SqlParameter("@Administrator_ID", SqlDbType.Char,14)};
            parameters[0].Value = Role_ID;
            parameters[1].Value = Administrator_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private List<UserRole> ChangeReaderToList(DataSet set)
        {
            List<UserRole> list = new List<UserRole>();
            DataTable table = set.Tables[0];
            foreach (DataRow row in table.Rows)
            {
                UserRole userRole = new UserRole();
                userRole.Administrator_ID = (string)row["Administrator_ID"];
                userRole.Role_ID = (string)row["Role_ID"];
                userRole.UserRole_Describe = row["UserRole_describe"].ToString();
                userRole.UserRole_Addtime = (DateTime)row["UserRole_addtime"];
                userRole.UserRole_Recoder = (string)row["UserRole_recoder"];
                userRole.UserRole_LastAmendTime = (DateTime)row["UserRole_lastAmendTime"];
                userRole.UserRole_LastAmendPerson = (String)row["UserRole_lastAmendPerson"];
                list.Add(userRole);
            }
            return list;
        }
    }
}
